import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { GeneralToolServiceProxy, GeneralToolListDto, StockOperationLogListDto, OutStocksInput, BackStocksInput } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';

import { finalize } from 'rxjs/operators';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';

@Component({
  selector: 'actionGeneralToolModal',
  templateUrl: './action-general-tool-modal.component.html'
})

export class ActionGeneralToolModalComponent extends AppComponentBase {

  @ViewChild('actionModal') modal: ModalDirective;
  @ViewChild('carDataTable') carDataTable: Table;
  @ViewChild('outDataTable') outDataTable: Table;
  @ViewChild('outPaginator') outPaginator: Paginator;
  @ViewChild('logDataTable') logDataTable: Table;
  @ViewChild('logPaginator') logPaginator: Paginator;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;

  operationNames = ['', '借出/配备', '返修', '送检', '技改'];
  title = '';
  type = 0;

  outStockInfo = '';
  outRemark = '';
  backStockInfo = '';
  backRemark = '';

  carGeneralTools = new Array<GeneralToolListDto>();
  selectCarGeneralTools = new Array<GeneralToolListDto>();
  outGeneralTools = new Array<GeneralToolListDto>();
  selectOutGeneralTools = new Array<GeneralToolListDto>();
  generalToolLogs = new Array<StockOperationLogListDto>();

  constructor(
    injector: Injector,
    private _generalToolService: GeneralToolServiceProxy
  ) {
    super(injector);
  }

  show(type: number): void {
    this.active = true;

    this.title = this.operationNames[type];
    this.type = type;

    // 初始化 信息
    this.outStockInfo = '';
    this.outRemark = '';
    this.backStockInfo = '';
    this.backRemark = '';

    // 初始化 dtos
    this.carGeneralTools = new Array<GeneralToolListDto>();
    this.selectCarGeneralTools = new Array<GeneralToolListDto>();
    this.outGeneralTools = new Array<GeneralToolListDto>();
    this.selectOutGeneralTools = new Array<GeneralToolListDto>();
    this.generalToolLogs = new Array<StockOperationLogListDto>();

    this._generalToolService.getCarGeneralTools(type).subscribe(result => {
      this.carGeneralTools = result.items;
      this.modal.show();
    });

  }

  save(): void {
    this.saving = true;

    // this._generalToolService.createOrUpdateGeneralTool(this.generalTool)
    // .pipe(finalize(() => { this.saving = false; }))
    // .subscribe(() => {
    //   this.notify.info(this.l('SavedSuccessfully'));

    // });

    this.close();
    this.modalSave.emit(null);
  }

  close(): void {
    this.active = false;
    this.modal.hide();
  }

  outStocks() {
    const input = new OutStocksInput();
    input.ids = this.selectCarGeneralTools.map(el => {
      return el.id;
    });

    input.operation = this.type;
    input.outStockInfo = this.outStockInfo;
    input.outRemark = this.outRemark;

    this.saving = true;

    this._generalToolService.outStocks(input)
      .pipe(finalize(() => { this.saving = false; }))
      .subscribe(() => {
        this.notify.info('出库成功');
        this.close();
        this.modalSave.emit(null);
      });
  }

  backStocks() {
    const input = new BackStocksInput();
    input.ids = this.selectOutGeneralTools.map(el => {
      return el.id;
    });

    input.backStockInfo = this.backStockInfo;
    input.backRemark = this.backRemark;

    this.saving = true;

    this._generalToolService.backStocks(input)
      .pipe(finalize(() => { this.saving = false; }))
      .subscribe(() => {
        this.notify.info('入库成功');
        this.close();
        this.modalSave.emit(null);
      });
  }

  tabSelect(tab: number) {
    if (tab === 2) {
      this.getOutGeneralTools();
    } else if (tab === 3) {
      this.getGeneralToolLogs();
    }
  }

  getOutGeneralTools(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.outPaginator.changePage(0);
      return;
    }

    if (this.type === 0) {
      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._generalToolService.getOutGeneralTools(
      undefined,
      this.type,
      this.primengTableHelper.getSorting(this.outDataTable),
      this.primengTableHelper.getMaxResultCount(this.outPaginator, event),
      this.primengTableHelper.getSkipCount(this.outPaginator, event)
    ).subscribe(result => {
      this.primengTableHelper.totalRecordsCount = result.totalCount;
      this.outGeneralTools = result.items;
      this.primengTableHelper.hideLoadingIndicator();
    });
  }

  getGeneralToolLogs(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.logPaginator.changePage(0);
      return;
    }

    if (this.type === 0) {
      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._generalToolService.getGeneralToolLogs(
      undefined,
      this.type,
      this.primengTableHelper.getSorting(this.logDataTable),
      this.primengTableHelper.getMaxResultCount(this.logPaginator, event),
      this.primengTableHelper.getSkipCount(this.logPaginator, event)
    ).subscribe(result => {
      this.primengTableHelper.totalRecordsCount = result.totalCount;
      this.generalToolLogs = result.items;
      this.primengTableHelper.hideLoadingIndicator();
    });
  }
}
